package com.by.remote.impl;

import com.by.remote.StockRemoteClient;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @Author 彭彦彬
 * @Date 2025/4/2 16:07
 * @注释
 */

@Component
public class StockRemoteClientFallbackFactory implements FallbackFactory<StockRemoteClient> {

    @Override
    public StockRemoteClient create(Throwable cause) {
        return new StockRemoteClient() {
            @Override
            public Map query(int id) {
                // 记录异常日志
                System.err.println("调用 StockRemoteClient.query 失败，异常信息：" + cause.getMessage());

                // 创建返回结果
                Map<String, Object> map = new HashMap<>();
                if (cause instanceof java.net.SocketTimeoutException) {
                    map.put("error", "请求超时，请稍后重试");
                } else if (cause instanceof feign.RetryableException) {
                    map.put("error", "服务暂时不可用，请稍后重试");
                } else {
                    map.put("error", "系统异常，请联系管理员");
                }
                return map;
            }
        };
    }
}